package com.autumn.redis;

/**
 * 操作抽象
 * 
 * @author 老码农
 *         <p>
 *         Description
 *         </p>
 * @date 2017-12-13 06:08:50
 */
abstract class AbstractOperations {
	/**
	 * 原子递减到最小值脚本
	 */
	final static byte[] SCRIPT_DECRBY_MIN_VALUE_BYTE;
	/**
	 * 读取后删除的脚本
	 */
	final static byte[] SCRIPT_READ_DELETE_BYTE;	
	/**
	 * 通配符匹配删除
	 */
	final static byte[] SCRIPT_MATCHES_DELETE_BYTE;
	
	static {
		String script = "local v = redis.call('get',KEYS[1]); if type(v) == 'string' and v > ARGV[1] then local r = redis.call('decrby',KEYS[1],ARGV[2]);return {1,r} else return {0,v} end;";
		SCRIPT_DECRBY_MIN_VALUE_BYTE = AutumnRedisTemplate.STRING_SERIALIZER.serialize(script);
		script = "local v = redis.call('get',KEYS[1]); redis.call('del', KEYS[1]);return v";
		SCRIPT_READ_DELETE_BYTE = AutumnRedisTemplate.STRING_SERIALIZER.serialize(script);		
		script = "local keys = redis.call('keys', ARGV[1]) for i=1,#keys,5000 do redis.call('del', unpack(keys, i, math.min(i+4999, #keys))) end return #keys";
		SCRIPT_MATCHES_DELETE_BYTE = AutumnRedisTemplate.STRING_SERIALIZER.serialize(script);
	}
}